package com.fingard.dsp.bank.directbank.gycb01;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

public class GYCB01Base extends DirectBase {

    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected long getTimeWaitForReq() {
        return Long.valueOf(getBankConfig().getValueAsString("timeWaitForReq"));
    }

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime17Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, GYCB01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.transName)) {
            throw new Exception("未赋值交易名称transName");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.batchID)) {
            throw new Exception("未赋值批次号batchID");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.transCount)) {
            throw new Exception("未赋值请求笔数transCount");
        }

        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("transaction");
        xmlWriter.writeStartElement("RequestHeader");
        xmlWriter.writeElementString("transName", p_bankHeader.transName);
        xmlWriter.writeElementString("language", "zh-cn");
        Date timeNow = p_bankHeader.clientTime == null ? new Date() : p_bankHeader.clientTime;
        DateFormat tTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        xmlWriter.writeElementString("clientTime", tTimeFormat.format(timeNow).replace(" ", "T"));
        xmlWriter.writeElementString("userID", bankFront.org);
        xmlWriter.writeElementString("userPassword", bankFront.loginPasswd);
        xmlWriter.writeElementString("operatorID", bankFront.loginName);
        xmlWriter.writeElementString("batchID", p_bankHeader.batchID);
        xmlWriter.writeElementString("transCount", p_bankHeader.transCount);
        xmlWriter.writeEndElement();//end RequestHeader
        xmlWriter.writeStartElement("RequestBody");
    }

    protected String[] sendToBank(String p_reqStr,String transType,String commandCode) throws Exception {
        String[] retStr = new String[]{"", ""};
        //后置挡板
        if (StringHelper.hasAnyChar(transType) && StringHelper.hasAnyChar(commandCode)) {
            this.detailSimulateForBank(bankFront.bankCode, transType, commandCode, p_reqStr, retStr);
            if (retStr[1].length() > 0) {
                return retStr;
            }
        }
        return sendToBank(p_reqStr);
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        if (bankFront.lastVisitTime != null) {
            long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
            long timeWaitForReq = getTimeWaitForReq();
            if (timeSpan < timeWaitForReq) {
                long sleepMils = timeWaitForReq - timeSpan;
                this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                Thread.sleep(timeWaitForReq - timeSpan);
            }
        }
        String[] retStr = new String[]{"", ""};
        String tmpUrl = bankFront.serverURL;//"http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/B2E/Forward.do?SIGDATA=1&userPassword=" + bankFront.loginPasswd;
        WriteBankLogLn("2.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(100000);
        tmpWebRequest.setRequestProperty("accept", "*/*");
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        tmpWebRequest.setRequestProperty("connection", "close");
        //tmpWebRequest.setRequestProperty("Expect", "100-continue");
        //tmpWebRequest.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        bankFront.lastVisitTime = new Date();
        return retStr;
    }
}
